/*
 * This is associated node used in the second level data structure of Range Tree.
 * It is inherited from Node, but added a few methods.
 */

package MyAssoTree;

import java.awt.Point;
import java.util.*;

import Red_Black_Tree.Node;

public class MyAssoNode extends Node {
	
	private Point key;
	
	public MyAssoNode(int x, int y){
		super();
		key = new Point(x, y);
	}
	public MyAssoNode() {
		super();
	}
	
	public int getX(){
		return key.x;
	}
	public int getY(){
		return key.y;
	}
	
	public ArrayList<MyAssoNode> getRSubTree(){
		if(this.getRChild().isLeaf())
			return null;
		else{
			ArrayList<MyAssoNode> sub = new ArrayList<MyAssoNode>();
			Stack<MyAssoNode> stack = new Stack<MyAssoNode>();
			stack.push((MyAssoNode) this.getRChild());
			MyAssoNode n;
			
			while(!stack.isEmpty()){
				n = stack.pop();
				sub.add(n);
				if(!n.getLChild().isLeaf())
					stack.push((MyAssoNode)n.getLChild());
				if(!n.getRChild().isLeaf())
					stack.push((MyAssoNode)n.getRChild());
			}
			
			return sub;
		}
	}
	
	public ArrayList<MyAssoNode> getLSubTree(){
		if(this.getLChild().isLeaf())
			return null;
		else{
			ArrayList<MyAssoNode> sub = new ArrayList<MyAssoNode>();
			Stack<MyAssoNode> stack = new Stack<MyAssoNode>();
			stack.push((MyAssoNode) this.getLChild());
			MyAssoNode n;
			
			while(!stack.isEmpty()) {
				n = stack.pop();
				sub.add(n);
				if(!n.getLChild().isLeaf())
					stack.push((MyAssoNode)n.getLChild());
				if(!n.getRChild().isLeaf())
					stack.push((MyAssoNode)n.getRChild());
			}
			
			return sub;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see Red_Black_Tree.Node#compareTo(Red_Black_Tree.Node)
	 * DEFINE: If current key is larger than n's key, return 1;
	 *         If current key is equal to n's key, return 0;
	 *         If current key is less than n's key, return -1.
	 * This version compares two points according to their y coordinate.
	 * For points with same y coordinate, the one with bigger x coordinate is of priority.
	 */
	@Override
	public int compareTo(Node n) {
		MyAssoNode n2 = (MyAssoNode) n;
		if (this.key.y > n2.getY())
			return 1;
		else if (this.key.y < n2.getY())
			return -1;
		else if (this.key.y == n2.getY() && this.key.x > n2.getX())
				return 1;
		else if (this.key.y == n2.getY() && this.key.x < n2.getX())
				return -1;
		else
				return 0;
	}
	public int compareTo (Point k2){
		if (key.y > k2.y)
			return 1;
		else if(key.y < k2.y)
			return -1;
		else if (key.y == k2.y && key.x > k2.x)
			return 1;
		else if (key.y == k2.y && key.x < k2.x)
			return -1;
		else
			return 0;
	}
}
